//
//  WJImageAutoSizeDemo3Controller.m
//  JerryIos
//
//  Created by 王军 on 2018/2/14.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJIASDemo3Controller.h"
#import "XHWebImageAutoSize.h"
#import "UIImageView+WebCache.h"
#import "WaterfallCollectionLayout.h"
#import "WJIASDemo3Cell.h"
#import "WJIASData1.h"
#import "MJExtension.h"

static NSString *const cellId = @"WJIASDemo3Cell";

@interface WJIASDemo3Controller () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property(nonatomic,strong) WaterfallCollectionLayout *layout;
@property(nonatomic,strong) NSArray *itemArray;

@end

@implementation WJIASDemo3Controller

-(NSArray *)itemArray{
    if(!_itemArray){
        //第一种方式
        NSString *json = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image02" ofType:@"json"]
                                                   encoding:NSUTF8StringEncoding
                                                      error:nil];
        WJIASData1 *iasData = [WJIASData1 mj_objectWithKeyValues:json];
        _itemArray = iasData.data;
        
        //第二种方式
//        NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image02" ofType:@"json"]];
//        NSDictionary *json =  [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
//        _itemArray = json[@"data"];
    }
    return _itemArray;
}

-(WaterfallCollectionLayout *)layout{
    if(!_layout){
        __weak __typeof(self) weakSelf = self;
        _layout = [[WaterfallCollectionLayout alloc]initWithItemsHeightBlock:^CGFloat(NSIndexPath *index) {
            return [weakSelf itemHeightAtIndexPath:index];
        }];
    }
    return _layout;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    [self.myCollectionView registerNib:[UINib nibWithNibName:@"WJIASDemo3Cell" bundle:nil] forCellWithReuseIdentifier:cellId];
    self.myCollectionView.collectionViewLayout = self.layout;
    
}

-(CGFloat )itemHeightAtIndexPath:(NSIndexPath *)indexPath{
    NSString *url = self.itemArray[indexPath.row];
    /**
     *  参数1:图片URL
     *  参数2:imageView 宽度
     *  参数3:预估高度,(此高度仅在图片尚未加载出来前起作用,不影响真实高度)
     */
    return [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:url] layoutWidth:self.layout.itemWidth estimateHeight:200];
}

#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WJIASDemo3Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    NSString *url = self.itemArray[indexPath.item];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        /**  缓存imageSize */
        [XHWebImageAutoSize storeImageSize:image forURL:imageURL completed:^(BOOL result) {
            /** reload */
            if(result) [collectionView xh_reloadDataForURL:imageURL];
        }];
        
    }];
    return cell;
}

@end
